'use strict';

/* Controllers */

function ExplorerCtrl($scope, $http) {
	$scope.loadContent = function(type, name) {
		var path = ''.concat(location.protocol, '//', location.host, location.pathname, '#/', type, '/', name);
		console.log('Loading path:', path);
		$('#content iframe').attr('src', path);
	}
	$scope.structure = new Object();
	$scope.tree = new Array();
	$http.get('json/structure.json').success(function(data) {

		if (window.console) { console.log('File System JSON has been loaded: ', data); }
		
		for ( var i in data ) {
			if ( data[i].type === 'folder' ) {
				var leaf = {
						key: i,
						title: data[i].attributes.name,
						isFolder: true,
						children: new Array(),
						type: 'folder'
				}
				$scope.tree.push(leaf);

			}
			for ( var j in data[i].elements ) {
				if (data[i].elements[j].type === 'folder') {					
					var leaf = {
						key: j,
						title: data[i].elements[j].attributes.name,
						isFolder: true,
						isLazy: true,
						children: new Array(),
						type: 'folder'
					}
					$scope.tree[0].children.push(leaf);
				
				}			
			}
		}
	})

}
ExplorerCtrl.$inject = ['$scope', '$http'];


function FolderCtrl($scope, $http, $routeParams){
	$http.get('js/load/' + $routeParams.name).success(function(data){
		alert(data);
	});
	$http.get('/' + $routeParams.name + '.json').success(function(data){
		$scope.list = data;
	});
	$scope.showDetails = function(id) {
		for (var i = 0; i < $scope.list.length; i++) {
			if (id == $scope.list[i].uid) { $scope.details = $scope.list[i]; }
			showModal();
		}
	}
}
FolderCtrl.$inject = ['$scope', '$http', '$routeParams'];

var showModal = function() {
	$('#modal').modal('show');
}
                          